﻿using System;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using PsTFS.Objects.Methodes;

namespace PsTFS.Objects
{
    /// <summary>
    /// Context applicatif
    /// </summary>
    public class Context
    {
        public Context() { }
        public Context(string tfsAdress)
        {
            this.Tfs = new TeamFoundationServer(tfsAdress, new UICredentialsProvider());

            this.Tfs.EnsureAuthenticated();
        }
        /// <summary>
        /// Commande Powershell en cours d'execution
        /// </summary>
        public Cmdlet Cmdlet { get; set; }

        /// <summary>
        /// Serveur TFS en cours d'utilisation
        /// </summary>
        private TeamFoundationServer m_Tfs;
        /// <summary>
        /// Serveur TSF
        /// </summary>
        public TeamFoundationServer Tfs
        {
            get
            {
                if (m_Tfs == null)
                {
                    PsTFSException ex = new PsTFSException();
                    ex.ObjectError = "Tfs";
                    throw ex;
                }
                return m_Tfs;
            }
            set
            {
                m_Tfs = value;
            }
        }

        /// <summary>
        /// Projet en cour
        /// </summary>
        private TeamProject m_Project;

        /// <summary>
        /// Projet en cour
        /// </summary>
        public TeamProject Project
        {
            get
            {
                //if (m_Project == null)
                //{
                //    Cmdlet.WriteError(new ErrorRecord(new ArgumentException("Not project object!!!"), "PsTFS", ErrorCategory.ObjectNotFound, "Project"));
                //    PsTFSException ex = new PsTFSException();
                //    ex.ObjectError = "Project";
                //    throw ex;
                //}
                return m_Project;
            }
            set
            {
                m_Project = value;
            }
        }
        /// <summary>
        /// Utilisateur actuellement connexté
        /// </summary>
        public string User { get; set; }

        /// <summary>
        /// Workspace utilisé dans le cas d'un répertoire mappé
        /// </summary>
        public Workspace m_Workspace;

        /// <summary>
        /// Workspace utilisé dans le cas d'un répertoire mappé
        /// </summary>
        public Workspace Workspace
        {
            get
            {
                if (m_Workspace == null)
                {
                    Workspace w = null;
                    try
                    {
                        w = Methode_Service.VCS().GetWorkspace(this.Cmdlet.CommandRuntime.Host.ToString());
                    }
                    catch (Exception ex) { Console.WriteLine(ex.ToString()); }

                    if (w == null)
                        // Creation du workspace. 
                        w = Methode_Service.VCS().CreateWorkspace(Guid.NewGuid().ToString(), Context.GetContext().User);
                    return w;
                }
                return m_Workspace;
            }
        }

        /// <summary>
        /// Variable privé pour le singleton
        /// </summary>
        private static Context m_GetContext;

        /// <summary>
        /// Récupération du context
        /// </summary>
        /// <returns>Retourne le context actuel</returns>
        public static Context GetContext()
        {
            if(m_GetContext == null)
                m_GetContext = new Context();

            return m_GetContext;

        }

        /// <summary>
        /// Vide le context
        /// </summary>
        public void ClearContext()
        {
            m_GetContext = null;
        }
    }
}
